package com.ruoyi.web.controller.scrap;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DeleteStatus;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.scrap.domain.ScrapApplyInfo;
import com.ruoyi.scrap.domain.ScrapApplyInto;
import com.ruoyi.scrap.domain.po.ScrapApplyIntoPo;
import com.ruoyi.scrap.domain.po.ScrapApplyPo;
import com.ruoyi.scrap.domain.vo.ScrapApplyVo;
import com.ruoyi.scrap.service.ScrapApplyInfoService;
import com.ruoyi.scrap.service.ScrapApplyIntoService;
import com.ruoyi.scrap.service.ScrapApplyService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/scrap/into")
public class ScrapApplyIntoController extends BaseController {

    @Resource
    ScrapApplyIntoService intoService;

    @Resource
    ScrapApplyInfoService applyInfoService;

    @GetMapping("/list")
    public TableDataInfo list(ScrapApplyIntoPo into)
    {
        startPage();
        List<ScrapApplyIntoPo> list = intoService.getScrapApplyInto(into);
        return getDataTable(list);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrapApplyIntoPo into)
    {
        List<ScrapApplyIntoPo> list = intoService.getScrapApplyInto(into);
        ExcelUtil<ScrapApplyIntoPo> util = new ExcelUtil<ScrapApplyIntoPo>(ScrapApplyIntoPo.class);
        util.exportExcel(response, list, "sheet1","报废物资出（入）库台账");
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(intoService.getInfo(id));
    }

    @PostMapping
    public AjaxResult add(@RequestBody ScrapApplyInto into) {

        if(ObjectUtils.isEmpty(into.getId())){

            into.setUserId(getUserId());
            into.setDeptId(getDeptId());
            into.setCreateBy(getUsername());
            into.setCreateTime(new Date());
            into.setStatus("0");
            into.setDelFlag(DeleteStatus.OK.getCode());

        }else{

            ScrapApplyInto applyInto = intoService.getById(into.getId());

            if(ObjectUtils.isEmpty(applyInto)){
                return AjaxResult.error("保存失败：未查询到要修改的入库单");
            }

        }

        into.setUpdateBy(getUsername());
        into.setUpdateTime(new Date());

        return toAjax(intoService.saveOrUpdate(into));
    }

    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {

        List<ScrapApplyInto> removes = Arrays.asList(ids).stream().map(x->{

            ScrapApplyInto into = new ScrapApplyInto();

            ScrapApplyInto applyInto = intoService.getById(x);

            if(ObjectUtils.isEmpty(applyInto)){
                throw new BaseException("删除失败：未查询到要删除的入库单");
            }

            into.setId(applyInto.getId());
            into.setDelFlag(DeleteStatus.DELETED.getCode());
            into.setUpdateBy(getUsername());
            into.setUpdateTime(new Date());

            return into;

        }).collect(Collectors.toList());

        if(ObjectUtils.isEmpty(removes)){
            return AjaxResult.error("删除失败：未查询到要删除的入库单");
        }

        return toAjax(intoService.updateBatchById(removes));
    }

    @GetMapping("/apply")
    public TableDataInfo apply(ScrapApplyPo apply)
    {
        startPage();
        apply.setWorkStaff(getUserId());
        List<ScrapApplyPo> list = applyInfoService.getScrapApplyInfo(apply);
        return getDataTable(list);
    }

}
